package com.glance.browser.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Size;

import org.springframework.format.annotation.DateTimeFormat;

@Entity
@Table(name="History")
public class History implements Serializable {

	private static final long serialVersionUID = 3173710152603772127L;

	@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER )
    	@JoinColumn(name="username")
    private User author;
	
	@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER )
	    @JoinColumn(name="class_id")
    private KnowledgeClass knowledgeClass;
	
	// Persistent Fields:
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "change_id")
    private Long changeId;
    
    @Column(name = "change_date")
	@Temporal(TemporalType.DATE)
	@DateTimeFormat(style = "M-")
	private Date changeDate;
    
    @Column(name = "description", length = 200)
    @Size(min=1)
    private String description;
    
    // Constructors:
    public History() {
    }

    public History(User author, KnowledgeClass knowledgeClass, Date changeDate, String description) {
        this.author = author;
        this.knowledgeClass = knowledgeClass;
        this.changeDate = changeDate;
        this.description = description;
     }
    
    // String Representation:
    @Override
    public String toString() {
        return changeDate + " - " + author + " - " + knowledgeClass + " - " + description;
    }

	public User getAuthor() {
		return author;
	}

	public void setAuthor(User author) {
		this.author = author;
	}

	public KnowledgeClass getKnowledgeClass() {
		return knowledgeClass;
	}

	public void setKnowledgeClass(KnowledgeClass knowledgeClass) {
		this.knowledgeClass = knowledgeClass;
	}

	public Long getChangeId() {
		return changeId;
	}

	public void setChangeId(Long changeId) {
		this.changeId = changeId;
	}

	public Date getChangeDate() {
		return changeDate;
	}

	public void setChangeDate(Date changeDate) {
		this.changeDate = changeDate;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}
